<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-navbar :is-back="false" title="通知" back-icon-color='#333333' height="50" :border-bottom="false">
			<text class="iconfont icon-fanhuijian2" style="font-size:30rpx; margin-left:30rpx; font-weight:bold;"
				@click="previous_pages"></text>
			<view class="" style="margin-left: 600rpx; display: flex;align-content: center;">
				<image src="../../static/tupian88.png" mode="widthFix" style="width: 30rpx;"
					@click="$toLink('../release_notice/release_notice',{})"></image>
			</view>
		</u-navbar>
		<u-mask :show="display">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="tanchu">
						确认删除草稿?
					</view>
					<view class="anniu">
						<view class="" @click="display=false">
							取消
						</view>
						<view class="" @click="delete_notification">
							确定
						</view>
					</view>
				</view>
			</view>
		</u-mask>
		<view class="topBox">
			<view v-for="(topItem,topIndex) in quanbu" :key="topIndex" :data-name="topIndex" @click="changeIndex"
				:class="[activeIndex==topIndex?'con':'']">
				<text></text>
				{{topItem}}
			</view>
		</view>
		<view class="list">
			<view class="layout">
				<view class="notify" v-for="(item,index) in notify_list" :key="index"
					@click="$toLink('../inform_detail/inform_detail',{id:item.school_notify_id})">
					<view class="left">
						<image :src="$toggleIcon(item.type)" mode="widthFix"></image>
					</view>
					<view class="right">
						<view class="top">
							<text style="width:550rpx;">{{item.title}}</text>
							<text class="iconfont icon-lajitong" style="font-size:30rpx; margin-top:13rpx;"
								@click.stop="tempDelete(item.school_notify_id)" v-show="activeIndex==4"></text>
						</view>
						<view class="center">
							{{item.textLength}}
						</view>
						<view class="bottom">
							<text>{{item.create_time}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	import request from '../../request.js'
	export default {
		data() {
			return {
				quanbu: ['全部', '校级', '年级', '班级', '草稿箱'],
				activeIndex: 0,
				notifyList: [],
				parameter: '',
				draft_list: [],
				display: false,
				status: 'loading',
				page: 0,
				last_page: '',
				delete_num: '',
				back_page: 1,
			}
		},
		onLoad(option) {
			if (option.id) {
				switch (option.id) {
					case 1:
						this.back_page = 1;
						break;
					case 2:
						this.back_page = 2;
						break;
					case 3:
						this.back_page = 3;
						break;
				}
			}
			this.schoolNotifyLists();
		},
		onReachBottom() { //滚动到底部触发该函数
			console.log('触发了吗')
			if (this.page >= this.last_page) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
			} //如果大于最后一页那么就中断函数

			this.page = ++this.page;

			setTimeout(() => {
				switch (this.activeIndex) {
					case 4:
						this.notifyTemp(this.page);
						break;
					default:
						this.schoolNotifyLists(this.activeIndex, this.page);
				}
			}, 2000)
		},
		computed: {
			notify_list() {
				switch (this.activeIndex) {
					case 4:
						if (this.draft_list) {
							this.draft_list.forEach(item => {
								if (item.text.length > 21) {
									this.$set(item, 'textLength', item.text.substr(0, 21) + '...');
								} else {
									this.$set(item, 'textLength', item.text);
								}
							})
							return this.draft_list;
						} else {
							return [];
						}
						break;
					default:
						if (this.notifyList) {
							this.notifyList.forEach(item => {
								if (item.text.length > 21) {
									this.$set(item, 'textLength', item.text.substr(0, 21) + '...');
								} else {
									this.$set(item, 'textLength', item.text);
								}
							})
							return this.notifyList;
						} else {
							return [];
						}
				}
			}
		},
		methods: {
			tempDelete(param) {
				this.display = true;
				this.delete_num = param;
			},
			//对返回上一级页面分情况跳转
			previous_pages() {
				switch (this.back_page) {
					case 1:
						this.$toLink('../career/career', {}, 3)
						break;
					case 2:
						uni.navigateBack({
							delta: 1
						})
						break;
					case 3:
						break;
				}
			},
			delete_notification() {
				this.display = false;
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/school_notify/tempDelete',
					method: 'post',
					data: {
						school_notify_id: this.delete_num,
					},
					success(res) {
						if (res.statusCode == 200) {
							_this.page = 0;
							_this.notifyTemp(1);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			changeIndex(e) {
				console.log(e.currentTarget.dataset.name)
				let index = e.currentTarget.dataset.name;
				this.activeIndex = index;
				switch (this.activeIndex) {
					case 4:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 0;
						this.notifyTemp(1);
						break;
					default:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 0;
						this.schoolNotifyLists(this.activeIndex, 1);
				}
			},

			//学校通知
			schoolNotifyLists(param, param2) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				let str = param ? param : 0;
				let pagination = param2 ? param2 : 1;
				switch (str) {
					case 0:
						this.parameter = '全部';
						break;
					case 1:
						this.parameter = '全校';
						break;
					case 2:
						this.parameter = '年级';
						break;
					case 3:
						this.parameter = '班级';
						break;
				}
				console.log(this.parameter);
				request({
					url: base_url + 'teacher/schoolNotify/lists',
					method: 'post',
					data: {
						type: this.parameter,
						page: pagination,
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.notifyList.length = 0;
								//没有数据的话那么就将数组清空
								_this.notifyList = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.notifyList = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.notifyList = [];
								} else {
									_this.notifyList = res.data.data.data;
								}
								console.log(_this.notifyList);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.notifyList = [..._this.notifyList, ...res.data.data.data];
								console.log(_this.notifyList);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.notifyList);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			//草稿箱
			notifyTemp(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				let pagination = param ? param : 1;
				request({
					url: base_url + 'teacher/school_notify/temp',
					method: 'post',
					data: {
						page: pagination,
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.draft_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.draft_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.draft_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.draft_list = [];
								} else {
									_this.draft_list = res.data.data.data;
								}
								console.log(_this.draft_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.draft_list = [..._this.draft_list, ...res.data.data.data];
								console.log(_this.draft_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.draft_list);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},

		}
	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F2F2F2;

		.warp {
			display: flex;
			align-items: flex-start;
			justify-content: center;
			height: 100%;

			.rect {
				margin-top: 450rpx;
				border-radius: 10rpx;
				width: 480rpx;
				height: 250rpx;
				background-color: #fff;

				.tanchu {
					width: 100%;
					height: 150rpx;
					font-size: 33rpx;
					color: #3E3E3E;
					text-align: center;
					line-height: 150rpx;
				}

				.anniu {
					width: 100%;
					height: 100rpx;
					display: flex;
					justify-content: space-around;
					align-items: center;

					view {
						border-radius: 30rpx;
						width: 150rpx;
						height: 60rpx;
						text-align: center;
						line-height: 60rpx;
						font-size: 26rpx;

						&:nth-child(1) {
							color: #2C78FE;
							margin-left: 15rpx;
							border: 1px solid #2C78FE;
							background: #fff;

						}

						&:nth-child(2) {
							color: #fff;
							margin-right: 15rpx;
							background: linear-gradient(to right, #2C78FE, #32B0FF);
						}


					}
				}
			}
		}

		.topBox {
			width: 100%;
			height: 65rpx;
			background: #fff;
			position: relative;
			display: flex;
			justify-content: space-evenly;

			view {
				height: 65rpx;
				line-height: 65rpx;
				width: 120rpx;
				text-align: center;
				font-size: 30rpx;
				color: #333333;

				text {
					display: none;
				}


			}

			.con {
				position: relative;
				height: 65rpx;
				line-height: 65rpx;
				color: #4784FF;
				width: 120rpx;

				text {
					display: block !important;
					position: absolute;
					width: 120rpx;
					height: 4rpx;
					background: #4784FF;
					bottom: 0rpx;
					left: 0rpx;
				}
			}

		}

		.list {
			width: 100%;
			overflow: hidden;

			.layout {
				width: 710rpx;
				background-color: #fff;
				margin-left: 20rpx;

				.notify {
					width: 690rpx;
					margin-left: 10rpx;
					height: 155rpx;
					background-color: #fff;
					border-bottom: 1rpx solid #F2F2F2;
					display: flex;
					justify-content: flex-start;

					.left {
						width: 70rpx;
						height: 155rpx;
						margin-left: 5rpx;
						position: relative;

						image {
							&:nth-child(1) {
								position: absolute;
								width: 70rpx;
								left: 2rpx;
								top: 29rpx;
							}
						}

						text {
							&:nth-child(2) {
								width: 25rpx;
								height: 25rpx;
								border-radius: 50%;
								background-color: #f00;
								text-align: center;
								line-height: 25rpx;
								color: #fff;
								font-size: 5rpx;
								position: absolute;
								z-index: 100;
								display: block;
								right: 0rpx;
								top: 25rpx;
							}
						}

						.vanish {
							display: none !important;
						}

					}

					.right {
						width: 590rpx;
						margin-left: 25rpx;
						height: 155rpx;
						overflow: hidden;

						.top {
							width: 100%;
							height: 50rpx;
							font-size: 28rpx;
							display: flex;
							justify-content: flex-start;
							color: #333333;
							margin-top: 19rpx;

							text {
								&:nth-child(1) {
									margin-top: 8rpx;
								}
							}
						}

						.center {
							width: 100%;
							font-size: 24rpx;
							color: #666666;
							height: 35rpx;
							// margin-top: 10rpx;
						}

						.bottom {
							width: 100%;
							height: 24rpx;
							color: #b7b7b7;
							font-size: 22rpx;
							margin-top: 12rpx;
						}

					}

					&:nth-child(1) {
						margin-top: 20rpx;
					}
				}
			}

		}
	}
</style>
